import './bootstrap';
import { createApp, h } from "vue";
import { createInertiaApp } from "@inertiajs/vue3";
import { createPinia } from "pinia";
import { ZiggyVue, route } from "../../vendor/tightenco/ziggy";
import * as bootstrap from "bootstrap";
import Layui from '@layui/layui-vue'
import '@layui/layui-vue/lib/index.css'
import { Button, Area, Rate, Dialog, Popup, PullRefresh, Form, Field, showToast, showConfirmDialog } from 'vant';
import 'vant/lib/index.css';
import { Icon} from 'vant';
import DashboardLayout from '@/Layouts/DashboardLayout.vue'; // 确保导入正确的布局组件
import InputField from '@/Pages/compontents/InputField.vue';
window.bootstrap = bootstrap;
window.route = route; // 定义全局route函数
const appName = import.meta.env.VITE_APP_NAME || "Laravel";

// 定义过滤需要导航栏的地址
const BarUrl = [
    'home/index/Index',
    'home/meixianghao/Index',
    'home/index/Index',
    'home/root/Index',
    'home/user/Index'
]

const pinia = createPinia();
createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: async (name) => {
        try {
            // 1. 使用正确的glob导入方式
            const pages = import.meta.glob('./Pages/**/*.vue', { eager: false });

            // 2. 获取页面路径
            const pagePath = `./Pages/${name}.vue`;
            const pageImport = pages[pagePath] || pages['./Pages/404.vue'];

            if (!pageImport) {
                throw new Error(`Page ${name} and 404 page not found`);
            }

            // 3. 执行动态导入
            const pageModule = await pageImport();

            // 4. 检查default导出
            if (!pageModule?.default) {
                console.error('Page module structure:', pageModule);
                throw new Error(`Page ${name} has no default export`);
            }
            if (BarUrl.includes(name)){
                // 这里是需要设置底部导航的设置
                pageModule.default.layout = pageModule.default.layout || DashboardLayout;
            }

            return pageModule;
        } catch (error) {
            console.error(`Error loading page ${name}:`, error);
            throw error;
        }
    },
    setup({ el, App, props, plugin }) {
        createApp({ render: () => h(App, props) })
            .use(plugin)
            .use(ZiggyVue)
            .use(pinia)
            .use(Layui)
            .use(Button)
            .use(Icon)
            .use(Area)
            .use(Rate)
            .use(Dialog)
            .use(Popup)
            .use(PullRefresh)
            .use(Form)
            .use(Field)
            .component('InputField', InputField)
            .mount(el);
    },
});